//
//  ButtonExt.swift
//  kano
//
//  Created by Frank on 2024/10/22.
//

import UIKit

extension UIButton {
    private struct AssociatedKeys {
        static var isIgnoringTouch = "isIgnoringTouch"
    }
    
    private var isIgnoringTouch: Bool {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.isIgnoringTouch) as? Bool ?? false
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.isIgnoringTouch, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    func preventDoubleTap(for seconds: TimeInterval = 2.0) {
        isIgnoringTouch = true
        isUserInteractionEnabled = false
        
        DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
            self.isIgnoringTouch = false
            self.isUserInteractionEnabled = true
        }
    }
}

